package com.ctshk.app.system.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.rpc.system.dto.PermissionDataDTO;
import com.ctshk.rpc.system.dto.PermissionUserMenuDTO;
import com.ctshk.rpc.system.req.PermissionDataReq;
import com.ctshk.rpc.system.req.QuickSetPermissionReq;
import com.ctshk.rpc.system.req.SysPermissionReq;
import com.ctshk.rpc.system.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2020-12-25
 */
@Api(value="权限", tags="权限管理")
@RestController
@RequestMapping("/permission")
public class SysPermissionController extends BaseController {

    @DubboReference
    private ISysPermissionService sysPermissionService;

    @ApiOperation(value="查询有效权限菜单", notes="查询有效权限菜单")
    @OperationLogAnnotation(operModul = OperationLogModel.PERMISSION, operType = OperationLogType.QUERY, operTitle = "查询有效权限菜单", operDesc = "查询有效权限菜单")
    @GetMapping("/query/menu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysType", value = "系统类型 1 核心业务系统;2 供应商开放平台;3 代理商分销系统ADS;4 地接系统;5 商旅系统TMC", required = true),
            @ApiImplicitParam(name = "menuId", value = "一级菜单id"),
            @ApiImplicitParam(name = "userId", value = "修改对象用户id获取查询他人的uid,传谁查谁", required = true)
    })
    public Result<PermissionUserMenuDTO> queryMenu(@RequestParam(name = "sysType", defaultValue = "1", required = false) Integer sysType, @RequestParam(name = "menuId", defaultValue = "0", required = false) Long menuId, @RequestParam long userId) {
        return Result.success(sysPermissionService.queryMenu(sysType, menuId, userId));
    }

    @ApiOperation(value="设置账号菜单权限", notes="设置账号菜单权限")
    @OperationLogAnnotation(operModul = OperationLogModel.PERMISSION, operType = OperationLogType.ADD, operTitle = "设置账号菜单权限", operDesc = "设置账号菜单权限")
    @PostMapping("/set")
    public Result setPermission(@RequestBody @Valid SysPermissionReq sysPermissionReq) {
        sysPermissionService.setMenuPermission(
                sysPermissionReq.getTopId(),
                sysPermissionReq.getIds(),
                sysPermissionReq.getUserId(),
                sysPermissionReq.getMenuType(),
                super.getCurrentUser(getRequest()).getId(),
                true
        );
        return Result.success();
    }

    @ApiOperation(value="设置账号功能按钮权限", notes="设置账号菜单功能按钮权限")
    @OperationLogAnnotation(operModul = OperationLogModel.PERMISSION, operType = OperationLogType.ADD, operTitle = "设置账号菜单功能按钮权限", operDesc = "设置账号菜单功能按钮权限")
    @PostMapping("/set-button")
    public Result setPermissionData(@RequestBody @Valid PermissionDataReq permissionDataReq) {
        sysPermissionService.setMenuPermissionData(permissionDataReq, super.getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="快速设置账号权限", notes="查询他人权限并快速同步设置指定账号权限")
    @OperationLogAnnotation(operModul = OperationLogModel.PERMISSION, operType = OperationLogType.UPDATE, operTitle = "快速设置账号权限", operDesc = "查询他人权限并快速同步设置指定账号权限")
    @PostMapping("/set-quick")
    public Result setQuick(@RequestBody @Valid QuickSetPermissionReq quickSetPermissionReq) {
        sysPermissionService.setQuick(quickSetPermissionReq.getOthersUserId(), quickSetPermissionReq.getUserId(), super.getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="查询指定菜单的数据权限范围", notes="查询指定菜单的数据权限类型和自定义范围")
    @OperationLogAnnotation(operModul = OperationLogModel.PERMISSION, operType = OperationLogType.QUERY, operTitle = "查询指定菜单的数据权限范围", operDesc = "查询指定菜单的数据权限类型和自定义范围")
    @GetMapping("/query/data")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单id", required = true),
            @ApiImplicitParam(name = "userId", value = "修改对象用户id或快速查询的用户id", required = true)
    })
    public Result<PermissionDataDTO> queryData(@RequestParam Long menuId, @RequestParam long userId) {
        return Result.success(sysPermissionService.queryPermissionData(menuId, userId));
    }

}
